//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    partial class FilterColumnResources
    {
        public FilterColumnResources()
        {
            InitializeComponent();
        }
        void OnOK(object sender, RoutedEventArgs args)
        {
            Button b = (Button)sender;
            ItemsControl tv = (ItemsControl) b.Tag;
            ValuesFilter vf = new ValuesFilter();
            foreach (FilterValue value in tv.ItemsSource)
            {
                if (!value.IsSelected)
                    continue;
                vf.Filters.Add(value.Value.ToString()); //\\
            }
            FilterColumnViewModel c = (FilterColumnViewModel)b.DataContext;
            c.Filter = vf;
            CloseSubMenu(sender);
        }

        void OnCancel(object sender, RoutedEventArgs args)
        {
            CloseSubMenu(sender);
        }

        private static void CloseSubMenu(object sender)
        {
            MenuItem mi = WindowServices.GetAncestorUsingTemplatedParent<MenuItem>(sender);
            MenuItem l = mi;
            while (l != null)
            {
                l = WindowServices.GetAncestorUsingTemplatedParent<MenuItem>(VisualTreeHelper.GetParent(l));
                if (l != null)
                    mi = l;
            }
            mi.IsSubmenuOpen = false;
        }
        void OnClearFilterColumn(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            FilterColumnViewModel m = (FilterColumnViewModel)((FrameworkElement)sender).DataContext;
            m.Filter = null;
        }
        void OnDisplayFilter(object sender, ExecutedRoutedEventArgs args)
        {
            FrameworkElement menu = (FrameworkElement) sender;
            ContentPresenter cp = menu.TemplatedParent as ContentPresenter;
            FilterColumnViewModel c = (FilterColumnViewModel)(menu).DataContext;
            CustomFilters f = new CustomFilters();
            f.First.Operator = (FilterOperator)args.Parameter;
            CustomAutoFilterControl w = new CustomAutoFilterControl();
            w.DataContext = f;
            WindowServices.ShowDialog(
                w, UIStringTable.CustomAutoFilter, cp,
                delegate
                    {
                        {
                            c.Model.Filter = f;
                        }
                    }, o => false);
        }


    }
}
